﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MTO.Framework.Telerik.UI.Grid;
using MTO.Library.Domain;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.Infrastructure;
using System.ComponentModel;
using MTO.Library.Web.Controllers.Customers.ViewModels;

namespace MTO.Library.Web.Controllers.Customers.Helpers
{
    public class CustomerIndexGridModelHelper : GridCustomBindingBase<Customer, CustomerIndexGridModel>
    {
        public CustomerIndexGridModelHelper(GridCommand command, IQueryable<Customer> dataSource)
            : base(command, dataSource)
        {
        }

        protected override Func<IEnumerable<CustomerIndexGridModel>, IEnumerable<AggregateFunctionsGroup>> AddGroupExpression(IQueryable<CustomerIndexGridModel> data, GroupDescriptor groupDescriptor, Func<IEnumerable<CustomerIndexGridModel>, IEnumerable<AggregateFunctionsGroup>> selector)
        {
            switch (groupDescriptor.Member)
            {
                case "FirstName":
                    return this.AddGroupExpression(data, x => x.FirstName, selector);
                case "LastName":
                    return this.AddGroupExpression(data, x => x.LastName, selector);
                default:
                    throw new NotSupportedException();
            }
        }

        protected override IQueryable<Customer> AddSortExpression(IQueryable<Customer> data, string memberName, ListSortDirection sortDirection, bool isFirst)
        {
            switch (memberName)
            {
                case "FirstName":
                    return this.AddSortExpression(data, x => x.FirstName, sortDirection, isFirst);
                case "LastName":
                    return this.AddSortExpression(data, x => x.LastName, sortDirection, isFirst);
                default:
                    throw new NotSupportedException();
            }
        }
    }
}
